<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 15/11/19
 * Time: 上午11:07
 */
$api_service_path = __DIR__ . "/../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();

////--基础维护
////发动机机油 221 Engine oil
//jd_car_product_brand_engine_oil_221 -- ok
////机油滤清器 211 Oil filter
//jd_car_product_brand_oil_filter_211 -- ok
////空气滤清器 209 Air cleaner
//jd_car_product_brand_air_cleaner_209 -- ok
////空调滤清器 213 Air conditioning filter
//jd_car_product_brand_air_conditioning_filter_213 -- ok
////燃油滤清器 212 Fuel filter
//jd_car_product_brand_fuel_filter_212 -- ok
////火花塞 214 Spark plug
//jd_car_product_brand_spark_plug_214 -- ok
////--轮胎专区
////前轮胎 218 Front tire
//jd_car_product_brand_front_tyre_218
////后轮胎 219 Rear tyre
//jd_car_product_brand_rear_tyre_219
////--蓄电池
////蓄电池 233 Battery
//jd_car_product_brand_battery_233
////--制动系统
////前刹车片 235 Front brake pad
//jd_car_product_brand_front_brake_pad_235
////后刹车片 236 Rear brake pad
//jd_car_product_brand_rear_brake_pad_236
////刹车油 249 Brake oil
//jd_car_product_brand_brake_oil_249
////前刹车盘 251 Front brake disc
//jd_car_product_brand_front_brake_disc_251
////后刹车盘 252 Rear brake disc
//jd_car_product_brand_rear_brake_disc_252
////--检修养护件
////主雨刷 197 The main wiper
//jd_car_product_brand_main_wiper_197
////副雨刷 222 Deputy wiper
//jd_car_product_brand_deputy_wiper_222
////汽油添加剂 220 Gasoline additive
//jd_car_product_brand_gasoline additive_220
////防冻液 248 Antifreeze
//jd_car_product_brand_antifreeze_248
////贴膜 250 Film
//jd_car_product_brand_film_250
////--照明系统
////近光灯 245 dipped lamp
//jd_car_product_brand_dipped_lamp_245
////远光灯 246 High lamp
//jd_car_product_brand_high_lamp_246
////雾灯 247 Fog lamp
//jd_car_product_brand_fog_lamp_247

$cars = DB::table('jd_car_data')
    ->where('id','>','0')
    ->select('id','car_model_id')
    ->get();
foreach($cars as $_car){
    $brands = queryExtendProperty('218',$_car->car_model_id);
    if($brands->code == 'success'){
        $data = $brands->data;
        foreach($data as $_data){
            if($_data->property->propertyName == '品牌'){
                $values = $_data->values;
                if(count($values) == 0){
                    $jd_car_product_brand = array(
                        'jd_car_id' => $_car->id,
                        'car_model_id' => $_car->car_model_id,
                        'brand' => 'null'
                    );
                    try{
                        DB::table('jd_car_product_brand_front_tyre_218')->insert($jd_car_product_brand);
                    }catch (Exception $e) {
                        echo "insert into jd_car_product_brand_front_tyre_218 error\n";
                        var_dump($e);
                    }
                }else{
                    foreach($values as $_value){
                        $jd_car_product_brand = array(
                            'jd_car_id' => $_car->id,
                            'car_model_id' => $_car->car_model_id,
                            'brand' => $_value->valueName
                        );
                        try{
                            DB::table('jd_car_product_brand_front_tyre_218')->insert($jd_car_product_brand);
                        }catch (Exception $e) {
                            echo "insert into jd_car_product_brand_front_tyre_218 error\n";
                            var_dump($e);
                        }
                    }
                }
            }
        }
    }else{
        echo $_car->id . ' ' . $brands->code . "\n";
        $jd_car_product_brand = array(
            'jd_car_id' => $_car->id,
            'car_model_id' => $_car->car_model_id,
            'brand' => 'null'
        );
        try{
            DB::table('jd_car_product_brand_front_tyre_218')->insert($jd_car_product_brand);
        }catch (Exception $e) {
            echo "insert into jd_car_product_brand_front_tyre_218 error\n";
            var_dump($e);
        }
    }
    usleep(500);
}

function queryExtendProperty($accyCatId,$car_model_id){
    $url = 'http://auto.jd.com/queryExtendProperty?accyCatId='.$accyCatId.'&modelId='.$car_model_id;
    while(1){
        try{
            $response = Httpful::get($url)
                ->timeoutIn(5)
                ->sendsForm()->send();
            break;
        }catch (Exception $e) {
            var_dump($e);
        }
    }
    return $response->body;
}